<?php
class ArticleControl extends CommonControl{
	function listAction(){
		$page = isset($_GET['page']) ? $_GET['page'] : 1;
		$page = is_numeric($page) ? ($page > 0 ? $page : 1) : 1;
		
		$sql = "select id,title,url,author,pick,add_time from article order by up_time desc";
		
		$array = M()->db->getRecordsByPage($page, $this->page_size, $sql);
		
		$this->out['pageCount'] = $array['pageCount'];
		$this->out['page'] = $array['page'];
		$this->out['pageDisplay'] = $this->pageDisplay;
		$this->out['data'] = $array['data'];
		$this->out['href'] = DOMAIN . '?act=article.list';
		$this->out['pagestring'] = $this->fetch("all.pages.html");
		$this->display('article.list.html');
	}
	
	
	function addAction(){
		$this->display('article.add.html');
	}
	
	function insertAction(){
		$title = trim($_POST['title']);
		$content = trim($_POST['content']);
		$re_arr = array();
		if(empty($title) || empty($content)){
			$re_arr['status'] = 2;
			echo json_encode($re_arr);
			return false;
		}
		$title_urlencode = urlencode($title);
		$youdao_url = C('youdao_url').$title_urlencode;
		$youdao_result = file_get_contents($youdao_url);
		$tran_arr = json_decode($youdao_result,true);
		$tran_str = $tran_arr['translation'][0];
		$title_url = createurl($tran_str);
		
		
		$insert_arr = array();
		$insert_arr['title'] = $title;
		$insert_arr['url'] = $title_url;
		$insert_arr['author'] = $_POST['author'];
		$insert_arr['source'] = $_POST['source'];
		$insert_arr['seo_keyword'] = $_POST['seo_keyword'];
		$insert_arr['seo_desc'] = mysql_escape_string($_POST['seo_desc']);
		$insert_arr['content'] = $_POST['content'];
		$insert_arr['pick'] = $_POST['pick'];
		if($_POST['pick'] == 1){
			$insert_arr['pick_time'] = time();
		}
		$insert_arr['add_time'] = time();
		M()->db->insert('article', $insert_arr);
		$re_arr['status'] = 1;
		echo json_encode($re_arr);
	}
	
	function editAction(){
		$id = $_GET['id'];
		$sql = "select * from article where id = {$id}";
		$data = M()->db->getRecord($sql);
		$this->out['data'] = $data;
		$this->display('article.edit.html');
	}
	function updateAction(){
		$article_id = trim($_POST['articleid']);
		$title = trim($_POST['title']);
		$content = trim($_POST['content']);
		$re_arr = array();
		if(empty($article_id) || empty($title) || empty($content)){
			$re_arr['status'] = 2;
			echo json_encode($re_arr);
			return false;
		}
		$update_arr = array();
		$update_arr['title'] = $title;
		$update_arr['author'] = $_POST['author'];
		$update_arr['source'] = $_POST['source'];
		$update_arr['seo_keyword'] = $_POST['seo_keyword'];
		$update_arr['seo_desc'] = mysql_escape_string($_POST['seo_desc']);
		$update_arr['content'] = $_POST['content'];
		$update_arr['pick'] = $_POST['pick'];
		if($_POST['pick'] == 1){
			$update_arr['pick_time'] = time();
		}
		M()->db->update('article',array('id'=>$article_id),$update_arr);
		$re_arr['status'] = 1;
		echo json_encode($re_arr);
	}
	function delAction(){
		$artcile_id = $_GET['artclieid'];
		if(empty($artcile_id) || !is_numeric($artcile_id)){
			echo '2';
			return false;
		}
		$del_sql = "delete from article where id={$artcile_id}";
		M()->db->execute($del_sql);
		echo '1';
		return true;
	}
}
?>
